package com.dadi.broadcastdemo1;

import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import java.util.Calendar;

import androidx.appcompat.app.AppCompatActivity;

public class Test2Activity extends AppCompatActivity {
    public static final String TAG = "liaoming";
    public static final String ACTION = "com.ddch.broadcast.action.noti";
    public static String id = "test_channel_01";
    NotificationManager nm;
    MainFragment2 mFragment2 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String info = "Nothing";
        Bundle extras = getIntent().getExtras();
        // check see if there is data in the bundle , ie launched from a notification
        if(extras != null){
          info = extras.getString("mytype");
        } // something wrong here
        Log.v(TAG, "info: " + info);
        mFragment2 = MainFragment2.newInstance(info);
        if(savedInstanceState == null){
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container2, mFragment2).commit();
        }
        createChannel();
    }

    /**
     *  for API 26+ create notification channels
     */
    private void createChannel() {
        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel mChannel = new NotificationChannel(id,
                    getString(R.string.channel_name), NotificationManager.IMPORTANCE_DEFAULT);
            mChannel.setDescription(getString(R.string.channel_description));
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            mChannel.setShowBadge(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            nm.createNotificationChannel(mChannel);
        }
    }
    public void setAlarm(){
        int NotID = 1;
        // --- use the AlarmManager to trigger an alarm ---
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        // --- sets the time for the alarm to trigger in 2 minutes from now ---
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 1);
        calendar.set(Calendar.SECOND, 0);

        Intent notificationIntent = new Intent(Test2Activity.ACTION);
        // 静态注册广播，intent 必须要添加包名
        notificationIntent.setPackage("com.dadi.broadcastdemo1");
        notificationIntent.putExtra("NotifiID", NotID);

        PendingIntent contentIntent = PendingIntent.getBroadcast(Test2Activity.this, NotID, notificationIntent, 0);
        Log.d(TAG, "Set alarm, I hope");
        // --- sets the alarm to trigger ----
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);
         sendBroadcast(notificationIntent); // let's see if it works... without the alarm
//        finish();   // exit the app
    }

}
